package com.ygy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ygy.entity.Evaluation;
import com.ygy.entity.VO.EvaluationVo;
import com.ygy.mapper.EvaluationMapper;
import com.ygy.service.EvaluationService;
import com.ygy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/evaluations")
public class EvaluationController {
    @Autowired
    private EvaluationService evaluationService;

    @Autowired
    private EvaluationMapper evaluationMapper;

    // 发表评论
    @PostMapping
    public Result addEvaluation(@RequestBody Evaluation evaluation) {
        boolean saveEvaluation = evaluationService.save(evaluation);
        if (saveEvaluation) {
            return Result.success(evaluation);//还不知道要返回什么
        }
        return Result.error("发布评论失败");
    }

    // 根据ID查询记录
    @GetMapping("/{orderId}")
    public Result<List<Evaluation>> getEvaluationById(@PathVariable Integer orderId) {
        QueryWrapper<Evaluation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",orderId); // 假设数据库中的字段名为user_id
        List<Evaluation> evaluations = evaluationMapper.selectList(queryWrapper);

        if (!evaluations.isEmpty()) {
            return Result.success(evaluations); // 返回带有评价列表的成功响应
        }
        return Result.error("没有找到该用户的评价");

    }

    //获取买家评论
    @GetMapping("buy/{orderId}/{buyerId}")
    public Result getBuyUserEvaluationById(@PathVariable Integer orderId,@PathVariable Integer buyerId) {
        QueryWrapper<Evaluation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",orderId)
                    .eq("buyer_id",buyerId); // 假设数据库中的字段名为user_id
        Evaluation evaluations = evaluationMapper.selectOne(queryWrapper);

        if (evaluations != null) {
            return Result.success(evaluations); // 返回带有评价列表的成功响应
        }
        return Result.error("没有找到该用户的评价");

    }
    //获取卖家评论
    @GetMapping("sell/{orderId}/{sellId}")
    public Result getSellUserEvaluationById(@PathVariable Integer orderId,@PathVariable Integer sellId) {
        QueryWrapper<Evaluation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",orderId)
                    .eq("seller_id",sellId); // 假设数据库中的字段名为user_id
        Evaluation evaluations = evaluationMapper.selectOne(queryWrapper);

        if (evaluations != null) {
            return Result.success(evaluations); // 返回带有评价列表的成功响应
        }
        return Result.error("没有找到该用户的评价");

    }
    //获取对他人对某个用户的所有评价
    @GetMapping("allEvaluation/{receiverId}")
    public Result<List<Evaluation>> getEvaluation(@PathVariable Integer receiverId) {
        QueryWrapper<Evaluation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receiver_id",receiverId); // 假设数据库中的字段名为user_id
        List<Evaluation> evaluations = evaluationMapper.selectList(queryWrapper);
        if (!evaluations.isEmpty()) {
            return Result.success(evaluations); // 返回带有评价列表的成功响应
        }
        return Result.error("没有找到该用户的评价");

    }
    //用vo获取评价计用户信息
    @GetMapping("evaluationVo/{receiverId}")
    public Result<List<EvaluationVo>> getEV( @PathVariable Integer receiverId){
      List<EvaluationVo>  evaluationVos = evaluationService.geEv(receiverId);
    return Result.success(evaluationVos);
    }
    //用评价id查询拿到订单号
    //获取买家评论
    @GetMapping("getEvaluationOrder/{evaluationId}")
    public Result getEvaluationOrder(@PathVariable Integer evaluationId) {
        QueryWrapper<Evaluation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("evaluation_id",evaluationId);
        Evaluation evaluations = evaluationMapper.selectOne(queryWrapper);

        if (evaluations != null) {
            return Result.success(evaluations.getOrderId()); // 返回带有评价列表的成功响应
        }
        return Result.error("没有找到该商品的评价");

    }

}
